package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DzWendabackMapper;
import com.ruoyi.system.domain.DzWendaback;
import com.ruoyi.system.service.IDzWendabackService;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 回答Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-07
 */
@Service
public class DzWendabackServiceImpl implements IDzWendabackService 
{
    @Autowired
    private DzWendabackMapper dzWendabackMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询回答
     * 
     * @param hfId 回答主键
     * @return 回答
     */
    @Override
    public DzWendaback selectDzWendabackByHfId(Long hfId)
    {
        return dzWendabackMapper.selectDzWendabackByHfId(hfId);
    }

    /**
     * 查询回答列表
     * 
     * @param dzWendaback 回答
     * @return 回答
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<DzWendaback> selectDzWendabackList(DzWendaback dzWendaback)
    {

        //权限控制
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
        int datascope = 5;
        for(SysRole role:listRole){
            if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                datascope=Integer.parseInt(role.getDataScope());
            }
        }
        dzWendaback.setDatascope(datascope);
        if(datascope==4){
            dzWendaback.setCheckDeptId(user.getDeptId());
        }
        if(datascope==5){
            DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
            if(huiyuan!=null){
                dzWendaback.setCheckHuiyuanId(huiyuan.getId());
            }else{
                dzWendaback.setCheckHuiyuanId(0l);
            }

        }
        return dzWendabackMapper.selectDzWendabackList(dzWendaback);
    }

    @Override
    public List<DzWendaback> selectDzWendabackListWeb(DzWendaback dzWendaback)
    {
        return dzWendabackMapper.selectDzWendabackList(dzWendaback);
    }

    /**
     * 新增回答
     * 
     * @param dzWendaback 回答
     * @return 结果
     */
    @Override
    public int insertDzWendaback(DzWendaback dzWendaback)
    {
        dzWendaback.setCreateTime(DateUtils.getNowDate());
        return dzWendabackMapper.insertDzWendaback(dzWendaback);
    }

    /**
     * 修改回答
     * 
     * @param dzWendaback 回答
     * @return 结果
     */
    @Override
    public int updateDzWendaback(DzWendaback dzWendaback)
    {
        return dzWendabackMapper.updateDzWendaback(dzWendaback);
    }

    /**
     * 批量删除回答
     * 
     * @param hfIds 需要删除的回答主键
     * @return 结果
     */
    @Override
    public int deleteDzWendabackByHfIds(Long[] hfIds)
    {
        return dzWendabackMapper.deleteDzWendabackByHfIds(hfIds);
    }

    /**
     * 删除回答信息
     * 
     * @param hfId 回答主键
     * @return 结果
     */
    @Override
    public int deleteDzWendabackByHfId(Long hfId)
    {
        return dzWendabackMapper.deleteDzWendabackByHfId(hfId);
    }
}
